package com.flyqiu.common.mybaits.plus.enums;

import com.flyqiu.common.enums.FlyQiuBaseStringEnum;
import com.flyqiu.common.mybaits.plus.QueryRuleModel;
import com.flyqiu.common.tools.FlyQiuCollectionTools;
import lombok.Getter;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 数据规则条件枚举类。
 */
@Getter
public enum FlyQiuQueryCondition implements FlyQiuBaseStringEnum {

    EQ("等于", "eq"),
    GT("大于", "gt"),
    GE("大于等于", "ge"),
    LT("小于", "lt"),
    LE("小于等于", "le"),
    NE("不等于", "ne"),
    BETWEEN("之间", "between"),
    NOT_BETWEEN("不在之间", "not_between"),
    LIKE("全模糊", "like"),
    NOT_LIKE("不包含模糊内容", "no_like"),
    LIKE_LEFT("左模糊", "like_left"),
    LIKE_RIGHT("右模糊", "like_right"),
    IN(" in ", "in"),
    NOT_IN(" not in ", "not_in"),
    IS_NULL("是 null", "is_null"),
    IS_NOT_NULL("不是 null", "is_not_null"),
    CUSTOM_SQL("自定义sql", "custom_sql"),
    ORDER_BY_ASC("排序正序", "asc"),
    ORDER_BY_DESC("排序倒序", "desc");

    /**
     * -- GETTER --
     * 获取标签。
     *
     * @return 标签
     */
    private final String label;
    /**
     * -- GETTER --
     * 获取值。
     *
     * @return 值
     */
    private final String value;

    /**
     * 构造方法。
     *
     * @param label 标签
     * @param value 值
     */
    FlyQiuQueryCondition(String label, String value) {
        this.label = label;
        this.value = value;

    }

    private static final Map<String, FlyQiuQueryCondition> cacheMap = new ConcurrentHashMap<>();


    static {
        FlyQiuCollectionTools.iterator(FlyQiuQueryCondition.values(),
                flyQiuQueryCondition -> cacheMap.put(flyQiuQueryCondition.getValue(), flyQiuQueryCondition));
    }

    /**
     * 根据代码创建枚举对象。
     *
     * @param code 条件代码
     * @return 枚举对象
     */
    public static FlyQiuQueryCondition create(String code) {
        return cacheMap.get(code);
    }

    /**
     * 条件匹配表达式的正则表达式字符串。
     */
    public static final String variableExpressionRegexStr = "^#\\{\\w+}";

    /**
     * 条件匹配表达式的正则表达式模式。
     */
    public static final Pattern variableExpressionRegexPattern = Pattern.compile(variableExpressionRegexStr);


    /**
     * 获取规则条件。
     *
     * @param colValue 条件值
     * @return 规则条件枚举
     */
    public static QueryRuleModel getRuleCondition(String colValue) {

        Matcher variableMatcher = variableExpressionRegexPattern.matcher(colValue);
        FlyQiuQueryCondition conditionEnum = FlyQiuQueryCondition.EQ;
        String colVal = colValue;
        if (variableMatcher.find()) {
            String group = variableMatcher.group();
            String replace = group.replace("#", "").replace("{", "").replace("}", "");
            conditionEnum = create(replace);
            if (conditionEnum == null) {
                conditionEnum = FlyQiuQueryCondition.EQ;
            }
            colVal = colValue.replace(group, "");
        }
        QueryRuleModel ruleModel = new QueryRuleModel();
        ruleModel.setCondition(conditionEnum);
        ruleModel.setColVal(colVal);
        return ruleModel;
    }
}
